<template>
    <div class="home">
        <div class="header">
            <div class="type-wrap" @click="toggle">
                <span class="title">{{currentSelect.name || '全部类型'}}</span>
                <i class="iconfont icon-quanbu"></i>
            </div>
            <div class="data-wrap">
                <span class="time" @click="monthToggle"> {{currentTime}} <i class="iconfont icon-xiajiantou"></i></span>
                <span class="expense">总支出￥1000</span>
                <span class="income">总收入￥2000</span>
            </div>
        </div>
        <div class="content-wrap">
            <van-pull-refresh v-model="refreshing" @refresh="onRefresh" success-text="刷新成功">
                <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
                    <CardItem v-for="item in list" :bill="item" :key="item"></CardItem>
                </van-list>
            </van-pull-refresh>
        </div>
        <PopType ref="popRef" @select="select"></PopType>
        <PopMonth ref="popMonthRef" @select="selectMonth"></PopMonth>
        <div class="add" @click="addToggle">
            <van-icon name="records" />
        </div>
        <PopAdd ref="addRef" @refresh="onRefresh"></PopAdd>
    </div>
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue';
import dayjs from 'dayjs';
import axios from '../utils/axios';

import PopType from '../components/PopType.vue';
import PopMonth from '../components/popMonth.vue';
import CardItem from '../components/CardItem.vue';
import PopAdd from '../components/PopAdd.vue';


export default {
    components: {
    PopType,
    PopMonth,
    CardItem,
    PopAdd
},
    setup() {
        const popRef = ref(null);
        const popMonthRef = ref(null);
        const addRef = ref(null);
        const state = reactive({
            page:1,
            totalPage:0,
            list:[],
            loading:false,
            finished:false,
            refreshing:false,
            currentSelect: {},
            currentTime: dayjs().format('YYYY-MM')
        })
        const toggle = () => {
            popRef.value.toggle()
        }
        const monthToggle = () => {
            popMonthRef.value.toggle()
        }
        const addToggle = () => {
            addRef.value.toggle()
        }

        const select = (item) => {
            state.currentSelect = item;
            console.log("--------"+ JSON.stringify(state.currentSelect));
            
        }

        const selectMonth = (item) => {
            state.currentTime = item;
            onRefresh()
        }

        const getBillList = async () => {
            // console.log(11);
            const {data} = await axios.get(`/bill/list?date=${state.currentTime}&type_id=${state.currentSelect.id || 'all'}&page=${state.page}&page_size=5`)
            console.log(data);
            // 下拉刷新的情况
            if (state.refreshing) {
                state.list = [];
                state.refreshing = false;
            }
            state.loading = false;
            state.list = state.list.concat(data.list);
            state.totalPage = data.totalPage;
            if (state.page >= state.totalPage) {
                state.finished = true;
            }
        }

        const onLoad = () => {
            // 上拉加载的情况 页数加一在调用
            if (!state.refreshing && state.page < state.totalPage) {
                state.page = state.page + 1;
            }
            // 刷新的情况直接调用
            getBillList();
        }

        const onRefresh = () => {
            state.finished = false;
            state.page = 1;
            state.refreshing = true;
            state.loading = true;
            onLoad()
        }
        // onMounted(() => {
        //     console.log("挂载");
        //     onRefresh()
        // })
        return {
            ...toRefs(state),
            popRef,
            addRef,
            toggle,
            select,
            popMonthRef,
            monthToggle,
            selectMonth,
            onRefresh,
            onLoad,
            addToggle
        }
    }
}
</script>

<style lang="scss" scoped>
.home {
    height: 100%;
    display: flex;
    flex-direction: column;
    padding-top: 80px;

    .header {
        position: fixed;
        top: 0;
        left: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        width: 100%;
        height: 80px;
        background-color: $primary;
        color: #fff;
        font-size: 14px;
        padding: 20px 20px;
        z-index: 100;

        .type-wrap {
            background-color: #50ca84;
            padding: 6px;
            border-radius: 4px;
            align-self: baseline;
            display: inline-block;
            position: relative;

            .title {
                margin-right: 22px;
            }

            .title::after {
                content: '';
                position: absolute;
                top: 12px;
                bottom: 11px;
                right: 32px;
                width: 1px;
                background-color: #e9e9e9;
            }

            .quanbu {
                font-size: 18px;
            }
        }

        .data-wrap {
            margin-top: 10px;
            font-size: 13px;

            .time {
                margin-right: 12px;
            }

            .xiajiantou {
                font-size: 12px;
            }

            .expense {
                margin-right: 10px;
            }
        }
    }
}
.add{
    position: fixed;
    bottom: 100px;
    right: 30px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid #e9e9e9;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    background-color: #fff;
    box-shadow: 0 0 10px 0 rgba(0,0,0,0.2);
    color: $primary;
}

.content-wrap {
    height: calc(100% - 50px);
    overflow: hidden;
    overflow-y: scroll;
    background-color: #f5f5f5;
    padding: 10px;
}
</style>